/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */

package com.tbit.uqbike.webmanager.util.http.rpc;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.tbit.Version;
import com.tbit.common.constant.SystemConstant;
import com.tbit.common.entity.TempPowerCheckResult;
import com.tbit.common.utils.TraceUtil;
import com.tbit.uqbike.constant.OperaPswConstant;
import com.tbit.uqbike.constant.ResponseCode;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.view.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 优驱
 *
 * @author Leon
 * 2019年11月17日 上午8:45:54
 */
@Slf4j

public class UQHttpClient {
    private static HttpClient client = null;

    /**
     * 维持单session
     */
    public static HttpClient getClient() {
        if (client == null) {
            MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
            client = new HttpClient(connectionManager);
            /**设置编码*/
            client.getParams().setContentCharset("UTF-8");
            /**请求超时*/
            client.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
            /**读取超时*/
            client.getHttpConnectionManager().getParams().setSoTimeout(5000);

            // uq http 请求客户端设置请求头参数，添加TraceId
            List<org.apache.commons.httpclient.Header> headers = new ArrayList<>();
            headers.add(new org.apache.commons.httpclient.Header(SystemConstant.TRACE_ID, TraceUtil.getTraceIdByLocal()));
            headers.add(new org.apache.commons.httpclient.Header("U-APP-VERSION", Version.VERSION));
            client.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
        }
        return client;
    }


    /**
     * 临时通电检查
     */
    public static TempPowerCheckResult checkTempPower(Map<String, Object> params) {
        String checkTempPowerUrl = OperaPswConstant.ACCOUNT_OPERA_CLIENT_URL + "/accountSpike/rpc/checkTempPower.do";

        if (log.isInfoEnabled()) {
            log.info("[UQHttpClient][checkTempPower][params: {}]", params);
        }

        String resp = UQHttpUtil.sendGet(checkTempPowerUrl, params);

        if (log.isInfoEnabled()) {
            log.info("[UQHttpClient][checkTempPower][result: {}]", resp);
        }

        JSONObject object = JSONUtil.parseObj(resp);

        if (ResponseCode.SUCC.equals(object.getInt(ResponseCode.RET))) {
            return object.get("data", TempPowerCheckResult.class);
        }

        return TempPowerCheckResult.cannotTempPower(object.getStr("msg"));
    }

    /**
     * 临时通电
     */
    public static void powerOn(Map<String, Object> params) {
        String tempPower = OperaPswConstant.ACCOUNT_OPERA_CLIENT_URL + "/terControl/rpc/tempPower.do";

        if (log.isInfoEnabled()) {
            log.info("[UQHttpClient][powerOn][params: {}]", params);
        }

        String resp = UQHttpUtil.sendPut(tempPower, params);

        if (log.isInfoEnabled()) {
            log.info("[UQHttpClient][powerOn][result: {}]", resp);
        }

        JsonResult jsonResult = JSONUtil.toBean(resp, JsonResult.class);

        if (ResponseCode.FAIL.equals(jsonResult.getRet())) {
            throw new BaseException(jsonResult.getMsg());
        }
    }
}